package cn.tkb.org.controller;

import cn.tkb.basic.exception.BusinessException;
import cn.tkb.basic.util.AjaxResult;
import cn.tkb.basic.util.PageQuery;
import cn.tkb.org.domain.Shop;
import cn.tkb.org.query.ShopQuery;
import cn.tkb.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;
    //新增和修改
    @RequestMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId() == null){
                shopService.add(shop);
            }else {
                shopService.update(shop);
            }
            return  AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("操作失败！！！");
        }
    }
    //查询店铺
    @GetMapping("/{id}")
    public Shop queryById(@PathVariable("id") Long id){
        return shopService.QeuryById(id);
    }
    //删除店铺
    @DeleteMapping("/{id}")
        public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败");
        }

    }
    //批量删除
    @PatchMapping
    public AjaxResult BatchDelete(@RequestBody List<Long> ids){
        try {
            shopService.BatchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.fail(e.getMessage());
        }
    }
    //查询所有店铺
    @GetMapping
    public List<Shop> queryAll(){
        return shopService.QeuryAll();
    }
    //高级查询和分页查询
    @PostMapping
    public PageQuery<Shop> queryPage(@RequestBody ShopQuery query){
       return shopService.findPage(query) ;
    }
    //注册店铺
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.success();
        }catch (BusinessException e) {//接收自定义的业务异常
            e.printStackTrace();
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.fail("系统繁忙稍后重试！！！");
        }
    }
}
